package com.example.demo.controller;


import com.auth0.jwt.JWT;
import com.example.demo.Service.ICollectService;
import com.example.demo.util.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/collect")
@CrossOrigin(origins = "*")
public class CollectController {

    @Autowired
    private ICollectService collectService;

    @PostMapping("/{productId}")
    public Map<String,Object> collect(@PathVariable Long productId ,HttpServletRequest request){

        int rs = collectService.collect(productId,JWTUtil.getUserId(request.getHeader("Authorization")));

        Map<String,Object> map = new HashMap<>();
        if (rs>0){

            map.put("code",200);
            map.put("msg","操作成功");

        }
        else {
            map.put("code",500);
            map.put("msg","操作失败");
        }

        return map;

    }

    @GetMapping("/select")
    public Map<String,Object> select(HttpServletRequest request){

        Map<String,Object> map = new HashMap<>();

        map.put("data",collectService.select(JWTUtil.getUserId(request.getHeader("Authorization"))));
        map.put("code",200);
        map.put("msg","查询成功");

        return map;
    }


}
